package com.swmutil.swmutil.util;

import com.swmutil.swmutil.pojo.All_Param;
import com.swmutil.swmutil.sqlite.AllParamSql;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;

public class ExcelReader {
    public static void main(String[] args) {
        String excelFilePath = "src/main/resources/小麦机通信矩阵格式.xlsx";
        readExcel(excelFilePath);
    }

    public static void readExcel(String excelFilePath) {
        try (FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
             Workbook workbook = new XSSFWorkbook(inputStream)) {

            int i = 0;
            Sheet sheet = workbook.getSheetAt(2); // 读取第一个工作表
            for (Row row : sheet) { // 迭代每一行
                int rowNum = row.getRowNum();
                if (rowNum > 0) {
                    All_Param allParam = new All_Param();
                    Cell cell6 = row.getCell(6);
                    if(cell6.getStringCellValue() == null || cell6.getStringCellValue() == ""){
                        continue;
                    }

                   /* Cell cell9 = row.getCell(9);
                    int bitlength = 0;
                    switch (cell9.getCellType()) {
                        case STRING:
                            System.out.print(cell9.getStringCellValue() + "\t");
                            bitlength=Integer.parseInt(cell9.getStringCellValue());
                            break;
                        case NUMERIC:
                            System.out.print(cell9.getNumericCellValue() + "\t");
                            bitlength=(int)cell9.getNumericCellValue();
                            break;
                        case BOOLEAN:

                            break;
                        case FORMULA:

                            break;
                        default:
                            break;
                    }
                    if(bitlength!=1){
                        continue;
                    }*/

                    for (Cell cell : row) { // 迭代每一列
                        //信号名称
                        int columnIndex = cell.getColumnIndex();
                        switch (columnIndex) {
                            case 6:
                                System.out.print(cell.getStringCellValue() + "\t");
                                allParam.setSignal_name(cell.getStringCellValue());
                                break;
                            case 7:
                                allParam.setSignal_description(cell.getStringCellValue());
                                break;
                            case 9:
                                switch (cell.getCellType()) {
                                    case STRING:
                                        System.out.print(cell.getStringCellValue() + "\t");
                                        if (cell.getStringCellValue() != "") {
                                            allParam.setBit_length(Integer.parseInt(cell.getStringCellValue()));
                                            if (Integer.parseInt(cell.getStringCellValue()) == 1) {
                                                allParam.setType(1);
                                                allParam.setOptions("有效,无效");
                                                allParam.setPage_id(1);
                                            } else  if (Integer.parseInt(cell.getStringCellValue()) == 2) {
                                                allParam.setType(2);
                                                allParam.setOptions("正常,开路,对地短路,对电短路");
                                                allParam.setPage_id(2);
                                            }  else {
                                                allParam.setType(3);
                                                allParam.setPage_id(2);
                                            }
                                        }
                                        break;
                                    case NUMERIC:
                                        System.out.print(cell.getNumericCellValue() + "\t");
                                        allParam.setBit_length((int) cell.getNumericCellValue());
                                        if ((int) cell.getNumericCellValue() == 1) {
                                            allParam.setType(1);
                                            allParam.setOptions("有效,无效");
                                            allParam.setPage_id(1);
                                        }else  if ((int) cell.getNumericCellValue() == 2) {
                                            allParam.setType(2);
                                            allParam.setOptions("正常,开路,对地短路,对电短路");
                                            allParam.setPage_id(2);
                                        }  else {
                                            allParam.setType(3);
                                            allParam.setPage_id(2);
                                        }

                                        break;
                                    case BOOLEAN:
                                        System.out.print(cell.getBooleanCellValue() + "\t");
                                        break;
                                    case FORMULA:
                                        System.out.print(cell.getCellFormula() + "\t");
                                        break;
                                    default:
                                        break;
                                }


                                break;
                            case 16:
                                if (cell.getStringCellValue() != "") {
                                    allParam.setValue(Integer.parseInt(cell.getStringCellValue()));
                                }
                                break;
                            case 17:
                                System.out.println(cell.getStringCellValue());
                                allParam.setUnit(cell.getStringCellValue());
                                break;
                        }

                        // }
                    }


                        allParam.setLayoutx((i/31)*300+10);
                        allParam.setLayouty((i%31)*25);


                    i++;
                    allParam.setParam_order(i);
                    AllParamSql.insertPage(allParam);
                    // 根据不同数据类型，以字符串形式输出数据
                 /*   switch (cell.getCellType()) {
                        case STRING:
                            System.out.print(cell.getStringCellValue() + "\t");
                            break;
                        case NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t");
                            break;
                        case BOOLEAN:
                            System.out.print(cell.getBooleanCellValue() + "\t");
                            break;
                        case FORMULA:
                            System.out.print(cell.getCellFormula() + "\t");
                            break;
                        default: break;
                    }*/
                }
                System.out.println(); // 换行
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
